package cn.itcast.gateway.ratelimit;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 用户IP作为桶名称，对同一个IP做限流
 * @author 虎哥
 */
@Component
public class IpKeyResolver implements KeyResolver {
    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        // 1.获取request
        ServerHttpRequest request = exchange.getRequest();
        // 2.获取用户IP
        String ip = request.getRemoteAddress().getHostName();
        // 3.返回
        return Mono.just("limit:ip:" + ip);// ip:127.0.0.1
    }
}
